package com.xyw.his00.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyw.his00.anno.OptionLog;
import com.xyw.his00.dto.CheckRequestDTO;
import com.xyw.his00.entity.ResponseData;
import com.xyw.his00.mapper.CheckRequestMapper;
import com.xyw.his00.pojo.dbpojo.CheckRequest;
import com.xyw.his00.service.Impl.dbImpl.CheckRequestServiceImpl;
import com.xyw.his00.utils.TimeUtils.LocalDateTimeRevertStringUtil;
import com.xyw.his00.vo.C_pay;
import com.xyw.his00.vo.PatientCheck;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/checkRequest/")
public class CheckRequestController {
    @Resource
    private CheckRequestMapper checkRequestMapper;
    @Resource
    private CheckRequestServiceImpl checkRequestService;

    @Autowired
    private LocalDateTimeRevertStringUtil util;


    @Transactional
    @OptionLog
    //医生诊疗：新增检查申请可以批量申请也可以申请一个
    @PostMapping("insertCheckRequest")
    public ResponseData insertCheckRequest(
        @RequestBody CheckRequestDTO[] checkRequestDTOS
    ) {
        System.out.println("批量插入检查申请执行成功");
        if (checkRequestDTOS.length == 0) {
            return ResponseData.FAIL;
        } else {
            CheckRequest[] checkRequests = CheckRequestDTO.revert(checkRequestDTOS);
            for (int i = 0; i < checkRequests.length; i++) {
                checkRequestMapper.insert(checkRequests[i]);
            }
            return  ResponseData.SUCCESS;
        }
    }
    //挂号收费员：搜索未交费的检查申请项目
    @RequestMapping("selectChecksNotPay")
   public List<C_pay> selectChecksNotPay(int registerId){
        System.out.println("selectChecksNotPay");
        return  checkRequestMapper.selectChecksNotPay("已开立",registerId);
   }
   //搜索患者检查项目信息
   @RequestMapping("selectPatientCheck")
    public List<PatientCheck> selectPatientCheck(int registerId){
       System.out.println("selectPatientCheck");
        return checkRequestMapper.selectPatientCheck(registerId);
   }
   //医生诊疗：申请结果：通过患者id搜索患者检查申请信息
    @RequestMapping("selectCheckRequestsByRegisterId")
    public List<CheckRequest> selectCheckRequestsByRegisterId(int registerId){
        System.out.println("搜索患者检查申请信息");
        QueryWrapper<CheckRequest> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("register_id",registerId);
        return checkRequestMapper.selectList(queryWrapper);
    }
    //根据挂号id和项目id搜索患者检查项目信息
    @RequestMapping("selectPatientCheckByTwo")
    public PatientCheck selectPatientCheckByTwo(int registerId,int medicalTechnologyId){
        System.out.println("selectPatientCheckByTwo");
        return checkRequestMapper.selectPatientCheckByTwo(registerId,medicalTechnologyId);
    }
   //根据患者id更新检查申请的状态
    @PutMapping("updateCheckRequestToPay")
    public ResponseData updateCheckRequestToPay(int registerId){
            QueryWrapper<CheckRequest> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("register_id",registerId);
          List<CheckRequest> checkRequests=checkRequestMapper.selectList(queryWrapper);
          for(CheckRequest checkRequest:checkRequests){
              checkRequest.setCheckState("已缴费");
          }
        if(checkRequests.isEmpty()){
            return ResponseData.SUCCESS;
        }else {
            return ResponseData.FAIL;
        }
    }
   //根据医生id搜索患者检查项目信息
    @RequestMapping("selectPatientCheckByDoctorId")
    public List<PatientCheck> selectPatientCheckByDoctorId(int doctorId){
        return checkRequestMapper.selectPatientCheckByDoctorId(doctorId);
    }
   //检查管理：搜索检查申请
    @RequestMapping("selectCheckRequestsNormal")
    public List<CheckRequest> selectCheckRequestsNormal(){
        QueryWrapper<CheckRequest> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("check_state","已开立");
        queryWrapper.select( " register_id ",
               " medical_technology_id",
               " check_info",
               " check_position",
               " creation_time",
                "check_employee_id",
               " inputcheck_employee_id",
                "check_time",
                "check_result",
                "check_state",
                "check_remark");
        return checkRequestMapper.selectList(queryWrapper);
    }
    //检查管理：更新检查结果
    @OptionLog
    @PostMapping("submitCheckRequestResult")
    public ResponseData submitCheckRequestResult(int registerId,
                                                 int medicalTechnologyId,
                                                 String checkResult,
                                                 int inputCheckEmployeeId,
                                                 String checkTime) throws ParseException {
        // 创建查询条件
        QueryWrapper<CheckRequest> checkRequestQueryWrapper = new QueryWrapper<>();
        checkRequestQueryWrapper.eq("register_id", registerId)
                .eq("medical_technology_id", medicalTechnologyId).
                select( " register_id ",
                        "medical_technology_id",
                        "check_info",
                        "check_position",
                        "creation_time",
                        "check_employee_id",
                        "inputcheck_employee_id",
                        "check_time",
                        "check_result",
                        "check_state",
                        "check_remark");
        // 根据查询条件查找CheckRequest对象
        CheckRequest checkRequest = checkRequestService.getOne(checkRequestQueryWrapper);

        if (checkRequest != null) {

            // 调用更新方法
           int l= checkRequestMapper.updateCheckRequest(checkResult,inputCheckEmployeeId,util.revertString(checkTime),registerId,medicalTechnologyId);

            if (l>0) {
                // 更新成功，返回相应的ResponseData对象
                return ResponseData.SUCCESS;
            } else {
                // 更新失败，返回相应的ResponseData对象
                return ResponseData.FAIL;
            }
        } else {
            // 未找到对应的CheckRequest对象，返回相应的ResponseData对象
            return ResponseData.ERROR;
        }
    }
}